<script setup lang="ts" name="FieldEnumValueAddEdit">
const AddEditFormRef = ref(null)

const emit = defineEmits(['save'])

const visible = ref(false)
const title = ref('')
const formData = ref({
  name: '',
  id: ''
})
const rules = ref({
  name: { required: true, message: '枚举值名称不能为空', trigger: ['blur', 'change'] },
  id: { required: true, message: '枚举值编号不能为空', trigger: ['blur', 'change'] }
})

// 显示新建/编辑部门弹窗
const showDialog = (v) => {
  visible.value = true
  if (v) {
    title.value = '编辑字段枚举值'
    formData.value.name = v.name
    formData.value.id = v.id
  } else {
    title.value = '添加字段枚举值'
  }
}
// 子组件将方法暴露给父组件
defineExpose({ showDialog })
const closeDialog = () => {
  AddEditFormRef.value.resetFields()
  formData.value.name = ''
  formData.value.id = ''
  visible.value = false
}

const handleOk = () => {
  if (!AddEditFormRef.value) return false
  AddEditFormRef.value.validate((valid) => {
    if (valid) {
      const result: any = {}
      result.name = formData.value.name
      result.id = formData.value.id
      emit('save', result)
      closeDialog()
    }
  })
}
</script>

<template>
  <el-dialog v-model="visible" width="600px" append-to-body destroy-on-close :title="title">
    <el-form ref="AddEditFormRef" label-width="auto" label-position="right" :model="formData" :rules="rules">
      <el-form-item label="编号" prop="id">
        <el-input v-model="formData.id" clearable placeholder="请输入枚举值编号" />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="formData.name" clearable placeholder="请输入枚举值名称" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="modal-footer" ref="modalFooter">
        <el-button @click="closeDialog">{{ $t('buttons.buttonCancel') }}</el-button>
        <el-button type="primary" @click="handleOk">{{ $t('buttons.buttonConfirm') }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss">
.el-form {
  padding: 40px 80px;
}

.modal-footer{
  display: inline-flex;
  overflow: hidden;
}
</style>
